package ui.components
{
	import events.GestureEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	
	import utils.ColorUtil;
	import utils.DisplayUtil;
	import utils.IClear;
	import utils.group.ISelectGroupItem;
	
    public class AbstractComponent extends Sprite implements IClear, ISelectGroupItem
    {
        protected var _selected:Boolean;
        protected var _selectedCallback:Function;
        protected var _enable:Boolean;
        protected var _func:Function;
        protected var _mouseDown:Boolean;
        protected var _mouseOver:Boolean;
        protected var _groupName:String;
        protected var _id:int;
        
        public function AbstractComponent(param1:int, param2:Function = null)
        {
            _id = param1;
            _enable = true;
            _selected = false;
            _mouseDown = false;
            _mouseOver = false;
            _func = param2;
            initTab();
        }

        public function get enable() : Boolean
        {
            return _enable;
        }

        public function set id(param1:int):void
        {
            this._id = param1;
        }

        public function get selectedCallback():Function
        {
            return _selectedCallback;
        }

        protected function downHandler(event:GestureEvent):void
        {
        	if(event.gestureTarget != this) return;
        	if(DisplayUtil.hitTestPoint(event.stageX, event.stageY, this)) {
	            _mouseDown = true;
        		if(mouseEnabled && enable && visible && stage != null)
	            {
	                ColorUtil.highLight(this);
	            }
        	}
        }
        protected function upHandler(event:GestureEvent):void
        {
        	if(event.gestureTarget != this) return;
            _mouseDown = false;
            if(DisplayUtil.hitTestPoint(event.stageX, event.stageY, this)) {
        		if(mouseEnabled && enable && visible && stage != null)
	            {
	                ColorUtil.deHighLight(this);
	            }
            }
        }
        protected function clickHandler(event:GestureEvent):void
        {
        	if(event.gestureTarget != this) return;
            if(DisplayUtil.hitTestPoint(event.stageX, event.stageY, this)) {
            	if(visible && stage != null) {
	        		if (_func != null)
	        		{
	        		    _func(event);
	        		}
            	}
            }
        }
        protected function moveHandler(event:GestureEvent):void
		{
        	if(event.gestureTarget != this) return;
        	if(DisplayUtil.hitTestPoint(event.stageX, event.stageY, this)) {
        		if(!_mouseOver) {
	        		_mouseOver = true;
		        	if (mouseEnabled && !selected && enable && visible && stage != null && !_mouseDown)
					{
						ColorUtil.highLight(this);
		            }
	        	}
        	} else {
        		if(_mouseOver) {
	        		_mouseOver = false;
	        		if (mouseEnabled && !selected && enable && visible && stage != null && !_mouseDown)
					{
						ColorUtil.deHighLight(this);
					}	
        		}
        	}
        }

        public function set enable(param1:Boolean) : void
        {
            _enable = param1;
            if (param1)
            {
                ColorUtil.deFadeColor(this);
            }
            else
            {
                ColorUtil.fadeColor(this);
                ColorUtil.deHighLight(this);
            }
        }

        public function clear() : void
        {
            
        }

        public function set groupName(param1:String) : void
        {
            _groupName = param1;
        }

        public function addListener(dispatcher:EventDispatcher) : void
        {
        	dispatcher.addEventListener(GestureEvent.TOUCH, downHandler);
        	dispatcher.addEventListener(GestureEvent.LEAVE, upHandler);
        	dispatcher.addEventListener(GestureEvent.MOVE, moveHandler);
        	dispatcher.addEventListener(GestureEvent.CLICK, clickHandler);
        }

        public function removeListener(dispatcher:EventDispatcher) : void
        {
        	dispatcher.removeEventListener(GestureEvent.TOUCH, downHandler);
        	dispatcher.removeEventListener(GestureEvent.LEAVE, upHandler);
        	dispatcher.removeEventListener(GestureEvent.MOVE, moveHandler);
        	dispatcher.removeEventListener(GestureEvent.CLICK, clickHandler);
        }

        public function get selected() : Boolean
        {
            return this._selected;
        }

        public function get id() : int
        {
            return this._id;
        }

        protected function initTab() : void
        {
            this.tabEnabled = false;
            this.tabChildren = false;
        }

        public function set selectedCallback(param1:Function) : void
        {
            _selectedCallback = param1;
        }

        public function get selectable() : Boolean
        {
            return enable;
        }

        public function get groupName() : String
        {
            return _groupName;
        }

        public function set selected(param1:Boolean) : void
        {
            this._selected = param1;
            if (param1)
            {
                ColorUtil.highLight(this);
            }
            else
            {
                ColorUtil.deHighLight(this);
            }
            if (param1 && _selectedCallback != null)
            {
                _selectedCallback(this);
            }
        }
    }
}
